package com.thinkTank.oa.util;

import java.util.Stack;

public class Jinzhi
{
    private static char[] array = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
            .toCharArray();
    private static String numStr = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
    
    /**
     * @author fengjingjing
     * @des 将十进制数值转换成需要的进制
     * @param number 要转换的十进制数值
     * @param N 需要转换成多少进制
     * @dete  2018/08/24
     * @return 
     */
    public static String _10_to_N(long number, int N)
    {
        Long rest = Long.valueOf(number);
        Stack<Character> stack = new Stack();
        StringBuilder result = new StringBuilder(0);
        while (rest.longValue() != 0L)
        {
            stack.add(Character.valueOf(array[new Long(rest.longValue() % N).intValue()]));
            rest = Long.valueOf(rest.longValue() / N);
        }
        while (!stack.isEmpty()) {
            result.append(stack.pop());
        }
        return result.length() == 0 ? "0" : result.toString();
    }

    
    /**
     * @author fengjingjing
     * @des 将任意进制数值转换成十进制
     * @param number 要转换的数值
     * @param N 要转换的数值是多少进制
     * @dete  2018/08/24
     * @return 
     */
    public static long N_to_10(String number, int N)
    {
        char[] ch = number.toCharArray();
        int len = ch.length;
        long result = 0L;
        if (N == 10) {
            return Long.parseLong(number);
        }
        long base = 1L;
        for (int i = len - 1; i >= 0; i--)
        {
            int index = numStr.indexOf(ch[i]);
            result += index * base;
            base *= N;
        }
        return result;
    }

    public static void main(String[] args)
    {
    	
        System.out.println(_10_to_N(10000, 2));
        System.out.println(N_to_10("10011100010000", 2));
    }
}
